package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.*;
import com.blb.service.*;
import com.blb.utils.DataTablePage;
import com.blb.utils.DataTablesPage;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 * 供应商
 *
 * @author tb
 * @since 2020-05-20
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Qualifier("supplierServiceImpl")
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private ISortService sortService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ICompanyService companyService;

    /**
     * 我的供应商
     *
     * @return
     */
    @RequiresPermissions("mysupplier")
    @ResponseBody
    @RequestMapping("my_supplier")
    public DataTablesPage mySupplier(Integer draw, Integer start, Integer length, String sname,String companyAddress) {
        List<Company> list = supplierService.getSupplier(ShiroUtils.getCompany(), start, length, sname,companyAddress);
        int count = supplierService.getSupplierCount(ShiroUtils.getCompany(),sname,companyAddress);
        return new DataTablesPage<Company>(draw, count, count, list);
    }

    /**
     * 商品分类树形
     *
     * @return
     */
    @RequiresPermissions("mysupplier")
    @ResponseBody
    @RequestMapping("sortTree")
    public List<Sort> sortTree(String id) {
        return sortService.list(new QueryWrapper<Sort>().eq("company_id",id));
    }

    /**
     * 商品信息
     *
     * @return
     */
    @RequiresPermissions("mysupplier")
    @ResponseBody
    @RequestMapping("product")
    public DataTablesPage product(String id,Integer draw, Integer start, Integer length,String sid,Integer sortType) {
        int count = supplierService.getProductCount(id,sid,sortType);
        List<Product> list = supplierService.getProduct(id, start, length,sid,sortType);
        return new DataTablesPage<Product>(draw, count, count, list);
    }

    /**
     * 订购商品
     * @param order
     * @return
     */
    @RequiresPermissions("mysupplier:order")
    @ResponseBody
    @RequestMapping("apply")
    public String applyOrder(Order order) {

        User user = (User) SecurityUtils.getSubject().getPrincipal();

        Product pro = supplierService.getProById(order.getProId());
        Company company = companyService.getById(user.getCompany().getId());

        String id = UUIDUtils.getId();
        String no = UUIDUtils.getIdByTopSix();
        order.setId(id);
        order.setOrderId(no);
        order.setOrderStatus("未下单");
        order.setPayment("未付款");
        order.setOrderTime(new Date());
        order.setOrderType("采购订单");
        order.setTakeAddress(company.getCompanyAddress());
        order.setCompanyId(user.getCompany().getId());

        System.out.println(pro);
        System.out.println(pro.getProductType());
        if ("普通商品".equals(pro.getProductType())){

            System.out.println("111111111111111111");

            order.setMoney(Float.toString(pro.getRetailPrice() * Float.parseFloat(order.getSum())));
        }else if ("积分商品".equals(pro.getProductType())){

            System.out.println("2222222222222222");
            order.setMoney(Float.toString(pro.getIntegral() * Float.parseFloat(order.getSum())));
        }
        System.out.println();order.getMoney();
        boolean result = orderService.save(order);
        if (result){
            return "{\"result\":true}";
        }else {
            return "{\"result\":false}";
        }
    }
}

